package com.gking.processHarvest.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gking.processHarvest.common.Res;
import com.gking.processHarvest.dto.VerifyAddFrindDto;
import com.gking.processHarvest.entity.Friends;
import com.gking.processHarvest.entity.Master;
import com.gking.processHarvest.service.Friends.FriendsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/friend")
public class FriendsController {

    @Autowired
    private FriendsService friendsService;


    /**
     * 搜索要添加的账号
     *
     * @param account 账号
     * @return s
     */
    @GetMapping("/search/{account}")
    public Res<VerifyAddFrindDto> search(@PathVariable String account) {
        log.warn("要校验的好友用户account 为：{}", account);
        return friendsService.search(account);
    }


    /**
     * 添加好友
     *
     * @param master 用户账号
     * @return s
     */
    @PostMapping("/add")
    public Res<String> add(@RequestBody Master master) {
        log.warn("要添加的好友用户account 为：{}", master.getAccount());
        return friendsService.add(master.getAccount());
    }


    /**
     * 删除好友
     *
     * @param id 好友用户id
     * @return s
     */
    @DeleteMapping("/delete/{id}")
    public Res<String> delete(@PathVariable String id) {
        log.warn("要删除的好友用户id 为：{}", id);
        return friendsService.delete(id);
    }


    /**
     * 获取好友列表
     *
     * @param page     页码
     * @param pageSize 大小
     * @param type     类型 0 为用户关注好友  1为用户粉丝好友
     * @return s
     */
    @GetMapping("/list")
    public Res<Page> get(Integer page, Integer pageSize, int type) {
        log.warn("page: {}", page);
        log.warn("pageSize: {}", pageSize);
        log.warn("type: {}", type);

        if (page == null) page = 1;
        if (pageSize == null) pageSize = 12;
        if (type == 0 || type == 1) return friendsService.get(type, page, pageSize);
        else return Res.error("type参数只能为 0 和 1");
    }


    /**
     * 获取好友信息
     *
     * @param friendId 好友id
     * @return s
     */
    @GetMapping("/info/{friendId}")
    public Res<Master> friendInfo(@PathVariable String friendId) {
        log.warn("friendId: {}", friendId);

        if(friendId.isEmpty()) return  Res.error("好友id不能为空");
        return friendsService.friendInfo(friendId);
    }
}
